package com.wangcanxuan.www.service.Impl;

import com.wangcanxuan.www.dao.mapper.LikeMapper;
import com.wangcanxuan.www.dao.mapper.UserMapper;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.Like;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.exception.ServiceException;
import com.wangcanxuan.www.proxy.DaoProxy;
import com.wangcanxuan.www.service.LikeService;
import com.wangcanxuan.www.util.SecurityUtil;

import static com.wangcanxuan.www.entity.dto.ResultConstants.SUCCESS_REQUEST_CODE;

/**
 * 点赞业务实现类
 *
 * @author HHLJ
 * @date 2022/04/08
 */
public class LikeServiceImpl implements LikeService {

    private final UserMapper USER_MAPPER = DaoProxy.getMapper(UserMapper.class);
    private final LikeMapper LIKE_MAPPER = DaoProxy.getMapper(LikeMapper.class);

    @Override
    public ResultInfo<String> likePeople(String userId, String chooseName) {
        // 获取被点赞人的个人信息
        User selectUser = new User();
        selectUser.setNickName(chooseName);
        selectUser = USER_MAPPER.selectOneByFields(selectUser);
        String likedUserId = selectUser.getId();
        Like selectLike = LIKE_MAPPER.selectOneByFields(new Like(userId, likedUserId));

        // 检测是否点过赞
        if (selectLike != null) {
            return ResultInfo.operateRejectWithMessage("您已为该用户点过赞！");
        }

        // 添加点赞信息
        Like like = new Like(userId, likedUserId);
        like.setId(SecurityUtil.uuidCreateId());
        int insert = LIKE_MAPPER.insert(like);
        if (insert != 1) {
            throw new ServiceException("点赞功能异常");
        }

        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode());
    }
    @Override
    public ResultInfo<Long> selectLikesForUser(String userId) {
        // 通过用户id获得被点赞的数量
        Long aLong = LIKE_MAPPER.selectLikesForUser(userId);
        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode(),aLong);
    }
}
